/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MasterSupplier.java
 *
 * Created on Aug 9, 2010, 9:39:41 PM
 */
package com.ivanbiz.gui;

import com.ivanbiz.dao.BarangDAO;
import com.ivanbiz.dao.SatuanDAO;
import com.ivanbiz.helper.CenterRenderer;
import com.ivanbiz.helper.RightRenderer;
import com.ivanbiz.helper.ServiceHelper;
import com.ivanbiz.impl.BarangDAOImpl;
import com.ivanbiz.impl.SatuanDAOImpl;
import com.ivanbiz.model.Barang;
import com.ivanbiz.model.Satuan;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author Aprisma
 */
public class LaporanStockBarang extends javax.swing.JInternalFrame {

    ServiceHelper serviceHelper = new ServiceHelper();
    BarangDAO barangDAO = new BarangDAOImpl();
    SatuanDAO satuanDAO = new SatuanDAOImpl();
    List<Barang> listBarang = new ArrayList<Barang>();
    List<Satuan> listSatuan = new ArrayList<Satuan>();
    NumberFormat numberFormat = NumberFormat.getInstance(Locale.US);

    /** Creates new form MasterSupplier */
    public LaporanStockBarang() {
        initComponents();
        updateData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelCari = new javax.swing.JPanel();
        textCariBarang = new javax.swing.JTextField();
        comboCari = new javax.swing.JComboBox();
        panelDaftarCustomer = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableBarang = new javax.swing.JTable();

        setBorder(new org.jdesktop.swingx.border.DropShadowBorder());
        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Laporan Stock Barang");

        panelCari.setBorder(javax.swing.BorderFactory.createTitledBorder("Cari Barang"));

        textCariBarang.setFont(new java.awt.Font("Tahoma", 1, 11));
        textCariBarang.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textCariBarangKeyReleased(evt);
            }
        });

        comboCari.setFont(new java.awt.Font("Tahoma", 1, 11));
        comboCari.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout panelCariLayout = new javax.swing.GroupLayout(panelCari);
        panelCari.setLayout(panelCariLayout);
        panelCariLayout.setHorizontalGroup(
            panelCariLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCariLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(comboCari, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textCariBarang, javax.swing.GroupLayout.DEFAULT_SIZE, 575, Short.MAX_VALUE))
        );
        panelCariLayout.setVerticalGroup(
            panelCariLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCariLayout.createSequentialGroup()
                .addGroup(panelCariLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textCariBarang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboCari, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        panelDaftarCustomer.setBorder(javax.swing.BorderFactory.createTitledBorder("Daftar Barang"));

        tableBarang.setAutoCreateRowSorter(true);
        tableBarang.setFont(new java.awt.Font("Tahoma", 1, 11));
        tableBarang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tableBarang);

        javax.swing.GroupLayout panelDaftarCustomerLayout = new javax.swing.GroupLayout(panelDaftarCustomer);
        panelDaftarCustomer.setLayout(panelDaftarCustomerLayout);
        panelDaftarCustomerLayout.setHorizontalGroup(
            panelDaftarCustomerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDaftarCustomerLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 734, Short.MAX_VALUE)
                .addContainerGap())
        );
        panelDaftarCustomerLayout.setVerticalGroup(
            panelDaftarCustomerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDaftarCustomerLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelDaftarCustomer, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelCari, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelCari, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelDaftarCustomer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-800)/2, (screenSize.height-550)/2, 800, 550);
    }// </editor-fold>//GEN-END:initComponents

    private void textCariBarangKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textCariBarangKeyReleased
        try {
            listBarang = barangDAO.getDataByLike(Barang.class, comboCari.getSelectedItem().toString(), textCariBarang.getText().toUpperCase());
            updateTableBarang();
        } catch (Exception ex) {
            Logger.getLogger(LaporanStockBarang.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_textCariBarangKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboCari;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel panelCari;
    private javax.swing.JPanel panelDaftarCustomer;
    private javax.swing.JTable tableBarang;
    private javax.swing.JTextField textCariBarang;
    // End of variables declaration//GEN-END:variables

    private void updateComboCari() {
        Object[] dataClass = serviceHelper.getPropertyClass(Barang.class);
        Object[] dataSearch = new Object[3];
        dataSearch[0] = dataClass[1];
        dataSearch[1] = dataClass[2];
        dataSearch[2] = dataClass[6];
        comboCari.setModel(new DefaultComboBoxModel(dataSearch));
    }

    private void updateData() {
        try {
            listBarang = barangDAO.getAll(Barang.class);
            updateTableBarang();
            updateComboCari();
        } catch (Exception ex) {
            Logger.getLogger(LaporanStockBarang.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void updateTableBarang() {
        String[] judul = {"No", "Kode", "Nama", "Harga", "Satuan", "Stock"};
        Object[][] isi = new Object[listBarang.size()][6];
        int x = 0;
        int no = 0;
        for (Barang barang : listBarang) {
            isi[x][0] = no += 1;
            isi[x][1] = barang.getKode();
            isi[x][2] = barang.getNama();
            isi[x][3] = "Rp " + numberFormat.format(barang.getHarga()) + ",00";
            isi[x][4] = barang.getSatuan().getNama();
            isi[x][5] = barang.getStock();
            x++;
        }
        tableBarang.setModel(new DefaultTableModel(isi, judul) {

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }
        });
        TableColumn column = tableBarang.getColumnModel().getColumn(0);
        column.setPreferredWidth(50);
        column.setMaxWidth(50);
        column.setCellRenderer(new CenterRenderer());
        TableColumn column1 = tableBarang.getColumnModel().getColumn(1);
        column1.setPreferredWidth(50);
        //column1.setMinWidth(100);
        column1.setCellRenderer(new CenterRenderer());
        TableColumn column3 = tableBarang.getColumnModel().getColumn(3);
        column3.setPreferredWidth(140);
        column3.setMaxWidth(140);
        column3.setCellRenderer(new RightRenderer());
        TableColumn column4 = tableBarang.getColumnModel().getColumn(4);
        column4.setPreferredWidth(125);
        column4.setMaxWidth(125);
        column4.setCellRenderer(new CenterRenderer());
        TableColumn column5 = tableBarang.getColumnModel().getColumn(5);
        column5.setPreferredWidth(75);
        column5.setMaxWidth(75);
        column5.setCellRenderer(new CenterRenderer());
    }
}
